package com.liangzhong.modules.ninehalf.mediator
{
	import com.adobe.serialization.json.JSON;
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Debug;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.HttpWebConst;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NineHalfEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.lobby.vo.RoomInfo;
	import com.liangzhong.model.lobby.vo.UserFortune;
	import com.liangzhong.model.lobby.vo.UserInfo;
	import com.liangzhong.model.ninehalf.vo.Bet;
	import com.liangzhong.model.ninehalf.vo.GameEnd;
	import com.liangzhong.model.ninehalf.vo.HostGold;
	import com.liangzhong.model.ninehalf.vo.NineHalfPlayer;
	import com.liangzhong.model.ninehalf.vo.NineHalfResult;
	import com.liangzhong.model.ninehalf.vo.OpenCard;
	import com.liangzhong.model.ninehalf.vo.SelectFirstCard;
	import com.liangzhong.model.ninehalf.vo.SelectHost;
	import com.liangzhong.model.ninehalf.vo.StartDisCard;
	import com.liangzhong.model.user.UserDetailInfo;
	import com.lz.football.module.ninehalf.INineHalfView;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	
	public class NineHalfMediator extends ModuleMediator
	{
		public static const MediatorName :String = "NineHalfMediator";
		/**
		 *游戏准备成功 
		 */		
		public static const NineHalf_Game_Ready_Success :String = "Client/NineHalf_Game_Ready_Success";
		/**
		 *游戏开始 
		 */		
		public static const NineHalf_Game_Start_Success :String = "Client/NineHalf_Game_Start_Success";
		/**
		 * 用户进入桌子
		 */		
		public static const NineHalf_User_Enter_Table :String = "Client/NineHalf_User_Enter_Table";
		/**
		 * 选庄
		 */		
		public static const NineHalf_Select_Host :String = "Client/NineHalf_Select_Host";
		/**
		 * 选庄金
		 */		
		public static const NineHalf_Select_Host_Gold :String = "Client/NineHalf_Select_Host_Gold";
		/**
		 * 选首发牌
		 */		
		public static const NineHalf_Select_First_Card :String = "Client/NineHalf_Select_First_Card";
		/**
		 * 发牌
		 */		
		public static const NineHalf_StartDis_Card :String = "Client/NineHalf_StartDis_Card";
		/**
		 * 轮到下注
		 */		
		public static const NineHalf_TrunBet :String = "Client/NineHalf_TrunBet";
		/**
		 * 下注结果
		 */		
		public static const NineHalf_Bet :String = "Client/NineHalf_Bet";
		/**
		 * 开牌
		 */		
		public static const NineHalf_OpenCard :String = "Client/NineHalf_OpenCard";
		/**
		 * 结算
		 */		
		public static const NineHalf_Balance :String = "Client/NineHalf_Balance";
		/**
		 *离开游戏 
		 */		
		public static const NineHalf_LeaveGame :String = "Client/NineHalf_LeaveGame";
		/**
		 *离开游戏 
		 */		
		public static const NineHalf_GiveUpHost :String = "Client/NineHalf_GiveUpHost";
		/**
		 *聊天
		 */		
		public static const NineHalf_Chat :String = "Client/NineHalf_Chat";
		/**
		 *结束
		 */		
		public static const NineHalf_GameEnd :String = "Client/NineHalf_GameEnd";
		/**
		 *交互
		 */		
		public static const NineHalf_Interactive :String = "Client/NineHalf_Interactive";
		
		
		public function NineHalfMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		/**
		 *获取NineHalfView 实例 
		 * @return 
		 * 
		 */		
		public function get nineHalfView():INineHalfView
		{
			return viewComponent as INineHalfView;
		}
		/**
		 * 注册事件
		 * 
		 */		
		override public function addEventListeners():void
		{
			(nineHalfView as DisplayObject).addEventListener(NineHalfEvent.NineHalf_StartGame_Req,onStartGameReq);
			AppEventDispatcher.getInstance().addEventListener(NineHalfEvent.NineHalf_SelectHostGold_Req,onSelectHostGoldReq);
			AppEventDispatcher.getInstance().addEventListener(NineHalfEvent.NineHalf_Bet_Req,onBet);
			(nineHalfView as DisplayObject).addEventListener(NineHalfEvent.NineHalf_ChangeTable_Req,onChangeTableReq);
			(nineHalfView as DisplayObject).addEventListener(NineHalfEvent.NineHalf_ContinueGame_Req,onContinueGameReq);
			(nineHalfView as DisplayObject).addEventListener(NineHalfEvent.NineHalf_GiveUpHost_Req,onGiveUpHostReq);
			AppEventDispatcher.getInstance().addEventListener(NineHalfEvent.NineHalf_Chat_Req,onChatReq);
			///
			AppEventDispatcher.getInstance().addEventListener(NineHalfEvent.NineHalf_Self_Balance_Complete,onSelfBalanceComplete);
		}
		/**
		 * 删除事件
		 * 
		 */		
		override public function removeEventListeners():void
		{
			(nineHalfView as DisplayObject).removeEventListener(NineHalfEvent.NineHalf_StartGame_Req,onStartGameReq);
			AppEventDispatcher.getInstance().removeEventListener(NineHalfEvent.NineHalf_SelectHostGold_Req,onSelectHostGoldReq);
			AppEventDispatcher.getInstance().removeEventListener(NineHalfEvent.NineHalf_Bet_Req,onBet);
			(nineHalfView as DisplayObject).removeEventListener(NineHalfEvent.NineHalf_ChangeTable_Req,onChangeTableReq);
			(nineHalfView as DisplayObject).removeEventListener(NineHalfEvent.NineHalf_ContinueGame_Req,onContinueGameReq);
			(nineHalfView as DisplayObject).removeEventListener(NineHalfEvent.NineHalf_GiveUpHost_Req,onGiveUpHostReq);
			AppEventDispatcher.getInstance().removeEventListener(NineHalfEvent.NineHalf_Chat_Req,onChatReq);
			///
			AppEventDispatcher.getInstance().removeEventListener(NineHalfEvent.NineHalf_Self_Balance_Complete,onSelfBalanceComplete);
		}
		/**
		 *开始游戏请求 
		 * @param e
		 * 
		 */		
		private function onStartGameReq(e : NineHalfEvent):void
		{
			this.sendNotification(ApplicationConstants.NineHalf_userReady);
		}
		/**
		 *选择庄金 
		 * @param e
		 * 
		 */		
		private function onSelectHostGoldReq(e : NineHalfEvent):void
		{
			this.sendNotification(ApplicationConstants.NineHalf_SelectHostGold,e.param);
		}
		/**
		 *下注请求 
		 * @param e
		 * 
		 */		
		private function onBet(e :NineHalfEvent):void
		{
			var betNum :int = int(e.param.betNum) ;
			var isQiu :int = int(e.param.isQiu);
			this.sendNotification(ApplicationConstants.NineHalf_Bet,{betNum:betNum,isQiu:isQiu});
		}
		/**
		 *换桌游戏 
		 * @param e
		 * 
		 */		
		private function onChangeTableReq(e:NineHalfEvent):void
		{
			nineHalfView.clearDesk();
			this.sendNotification(ApplicationConstants.NineHalf_ChangeTable);
		}
		/**
		 * 继续游戏请求
		 * @param e
		 * 
		 */		
		private function onContinueGameReq(e:NineHalfEvent):void
		{
			nineHalfView.clear();
			this.sendNotification(ApplicationConstants.NineHalf_ContinueGame);
		}
		/**
		 *下庄请求 
		 * @param e
		 * 
		 */		
		private function onGiveUpHostReq(e:NineHalfEvent):void
		{
			nineHalfView.clear();
			this.sendNotification(ApplicationConstants.NineHalf_GiveUpHost);
		}
		/**
		 *聊天请求 
		 * @param e
		 * 
		 */		
		private function onChatReq(e :NineHalfEvent):void
		{
			this.sendNotification(ApplicationConstants.NineHalf_Chat,e.param);
		}
		/**
		 *show balance 
		 * @param e
		 * 
		 */		
		private function onSelfBalanceComplete(e :NineHalfEvent):void
		{
			//this.sendNotification(NotificationConstants.Server_GetUserFortune_Success);
		}
		/**************************************         interface function              *************************************/
		/**
		 *游戏准备 
		 * 
		 */		
		protected  function gameReady():void
		{
			nineHalfView.gameReady(AppData.getInstance().nineHalfGame.curPos,AppData.getInstance().nineHalfGame.nineHalfTableInfo);
			for each(var nineHalfPlayer :NineHalfPlayer in AppData.getInstance().nineHalfGame.nineHalfPlayers)
			{
				nineHalfView.userEnterTable(nineHalfPlayer.uid,nineHalfPlayer.tablePos);
				this.sendNotification(HttpWebConst.Http_Lobby_GetUserProfile,{uids:"" + nineHalfPlayer.uid,fields:"uid,headurl1,username,nickname,gender,honour,wincnt,losecnt,mascot,gift",callBack:getUserDetailInfo});
			}
			//
			//super.gameReady();
		}
		/**
		 *用户进入桌子 
		 * @param nineHalfPlayer
		 * 
		 */		
		private function userEnterTable(nineHalfPlayer :NineHalfPlayer):void
		{
			if(nineHalfPlayer == null)
			{
				return;
			}
			nineHalfView.userEnterTable(nineHalfPlayer.uid,nineHalfPlayer.tablePos);
			////获取用户详细信息
			var uid :int = nineHalfPlayer.uid;
			this.sendNotification(HttpWebConst.Http_Lobby_GetUserProfile,{uids:"" + uid,fields:"uid,headurl1,username,nickname,gender,honour,wincnt,losecnt,mascot,gift",callBack:getUserDetailInfo});
		}
		/**
		 *个人资料返回 
		 * @param data
		 * 
		 */		
		private function getUserDetailInfo(data :Object):void
		{
			var res :String = String(data);
			var result :Object = JSON.decode(res)[0];
			if(result == null)
			{
				return;
			}
			var headUrl :String = result.headurl1;
			var uid :int = int(result.uid);
			var nineHalfPlayer :NineHalfPlayer = AppData.getInstance().nineHalfGame.getPlayerByUid(uid);
			if(nineHalfPlayer == null)
			{
				nineHalfPlayer = new NineHalfPlayer();
				//return;
			}
			//nineHalfPlayer.userName = result.username;
			//nineHalfPlayer.nickName = result.nickname;
			//nineHalfPlayer.headurl = headUrl;
			nineHalfPlayer.sex = result.gender;
			///
			nineHalfPlayer.uid = result.uid;
			nineHalfPlayer.username = result.username;
			nineHalfPlayer.nickname = result.nickname;
			///
			nineHalfView.setUserInfoData(nineHalfPlayer);
		}
		/**
		 *选庄 
		 * @param selectHost
		 * 
		 */		
		private function selectHost(selectHost :SelectHost):void
		{
			nineHalfView.selectHost(selectHost.fPoint,selectHost.sPoint,selectHost.tablePos);
		}
		/**
		 * 
		 * 显示庄金
		 */		
		private function showHostGold(hostGold :HostGold):void
		{
			nineHalfView.showHostGold(hostGold);
		}
		/**
		 *选择首发牌 
		 * @param selectFcard
		 * 
		 */		
		private function selectFirstCard(selectFcard :SelectFirstCard):void
		{
			nineHalfView.selectFirstCard(selectFcard.fPoint,selectFcard.sPoint,selectFcard.tablePos);
			nineHalfView.updateHostList(selectFcard.hostListPos);
		}
		/**
		 *开始发牌 
		 * @param startDCard
		 * 
		 */		
		private function startDisCard(startDCard :StartDisCard):void
		{
			nineHalfView.stardDisplayCard(startDCard.posTags);
		}
		/**
		 *轮到下注 
		 * @param betPos
		 * 
		 */		
		private function trunBet(betPos :int):void
		{
			nineHalfView.betTrun(betPos);
		}
		/**
		 *下注结果 
		 * @param bets
		 * 
		 */		
		private function bet(bets :Bet):void
		{
			nineHalfView.bet(bets.tablePos,bets.isQiu,bets.betNum);
		}
		/**
		 *开牌 
		 * @param opC
		 * 
		 */		
		private function openCard(opC :OpenCard):void
		{
			nineHalfView.openCard(opC.pokers);
		}
		/**
		 *结算 
		 * @param result
		 * 
		 */		
		private function balance(result :NineHalfResult):void
		{
			nineHalfView.balance(result.results);
			var userFortune :UserFortune ;//= AppData.getInstance().user.userFortune;
			if(userFortune == null)
			{
				return;
			}
			//userFortune.point = result.userPoint;
			//nineHalfView.setUserFortune(userFortune);
			var info :String = "User ID = " + AppData.getInstance().user.uid;
			if(userFortune.point != result.userPoint)
			{
				info += " Lobby UserPoint = " + userFortune.point;
				info += " Game UserPoint = " + result.userPoint;
				//Debug.logError(AppData.getInstance().user.uid,AppData.getInstance().user.key,info);
			}
		}
		/**
		 * 
		 * 更新财富
		 */		
		private function setUserFortune():void
		{
			var userFortune :UserFortune// = AppData.getInstance().user.userFortune;
			if(userFortune == null)
			{
				return;
			}
			nineHalfView.setUserFortune(userFortune);
		}
		/**
		 * 
		 * @param pos
		 * 
		 */		
		private function leaveGame(pos :int):void
		{
			nineHalfView.leaveGame(pos);
		}
		/**
		 *下庄 
		 * @param pos
		 * 
		 */		
		private function giveUpHost(pos :int):void
		{
			nineHalfView.giveUpHost(pos);
		}
		/**
		 *聊天 
		 * @param chat
		 */		
		/*private function chatSay(chat :NineHalfChat):void
		{
			if(chat == null)
			{
				return;
			}
			var nineHalfPlayer :NineHalfPlayer = AppData.getInstance().nineHalfGame.getPlayerByPos(chat.tablePos);
			if(nineHalfPlayer == null)
			{
				trace("Chat Can't find the player!");
				return;
			}
			chat.nickName = nineHalfPlayer.nickName;
			nineHalfView.chatSay(chat);
		}*/
		/**
		 *结束 
		 * 
		 */		
		private function gameOver(gend :Object):void
		{
			var gameEnd :GameEnd = gend as GameEnd;
			if(gameEnd == null)
			{
				return;
			}
			nineHalfView.gameEnd(gameEnd.hostNum);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [NineHalf_Game_Ready_Success,
					NineHalf_Game_Start_Success,
					NineHalf_User_Enter_Table,
					NineHalf_Select_Host,
					NineHalf_Select_First_Card,
					NineHalf_StartDis_Card,
					NineHalf_TrunBet,
					NineHalf_Bet,
					NineHalf_OpenCard,
					NineHalf_Balance,
					NotificationConstants.Server_GetUserFortune_Success,
					NineHalf_Select_Host_Gold,
					NineHalf_LeaveGame,
					NineHalf_GiveUpHost,
					NineHalf_Chat,
					NineHalf_GameEnd,
					NineHalf_Interactive,
					NotificationConstants.Server_WinInfo_Chat];
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case NineHalf_Game_Ready_Success://游戏准备成功
				{
					gameReady();
					break;
				}
				case NineHalf_Game_Start_Success://游戏开始
				{
					
					break;
				}
				case NineHalf_User_Enter_Table://用户进入桌子
				{
					userEnterTable(notification.getBody() as NineHalfPlayer);
					break;
				}
				case NineHalf_Select_Host://选庄
				{
					selectHost(notification.getBody() as SelectHost);
					break;
				}
				case NineHalf_Select_First_Card://选择首发牌
				{
					selectFirstCard(notification.getBody() as SelectFirstCard);
					break;
				}
				case NineHalf_StartDis_Card://发牌
				{
					startDisCard(notification.getBody() as StartDisCard);
					break;
				}
				case NineHalf_TrunBet://轮到下注
				{
					trunBet(notification.getBody() as int);
					break;
				}
				case NineHalf_Bet://下注结果
				{
					bet(notification.getBody() as Bet);
					break;
				}
				case NineHalf_OpenCard://开牌
				{
					openCard(notification.getBody() as OpenCard);
					break;
				}
				case NineHalf_Balance://结算
				{
					balance(notification.getBody() as NineHalfResult);
					break;
				}
				case NotificationConstants.Server_GetUserFortune_Success://更新财富
				{
					setUserFortune();
					break;
				}
				case NineHalf_Select_Host_Gold://选庄金
				{
					showHostGold(notification.getBody() as HostGold);
					break;
				}
				case NineHalf_LeaveGame://离开游戏
				{
					leaveGame(notification.getBody() as int);
					break;
				}
				case NineHalf_GiveUpHost://下庄
				{
					giveUpHost(notification.getBody() as int);
					break;
				}
				case NineHalf_Chat://聊天
				{
					//chatSay(notification.getBody() as NineHalfChat);
					break;
				}
				case NineHalf_GameEnd://结束
				{
					gameOver(notification.getBody());
					break;
				}
				case NotificationConstants.Server_WinInfo_Chat ://赢分
				{
					//nineHalfView.winInfoChat(notification.getBody() as Chat);
					break;
				}
				case NineHalf_Interactive://交互
				{
					nineHalfView.interactive(notification.getBody());
					break;
				}
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			addEventListeners();
			nineHalfView.initUI();
			//设置用户基本信息
			//var userInfo :UserInfo = AppData.getInstance().user.userInfo;
			//nineHalfView.setUserInfo(userInfo);
			////设置用户详细信息
			var userDetailInfo :UserDetailInfo = AppData.getInstance().user.userDetailInfo;
			if(userDetailInfo)
			{
				nineHalfView.setCurUserInfo(userDetailInfo);
			}
			else
			{
				////获取用户详细信息
				var uid :int = AppData.getInstance().user.uid;
				this.sendNotification(HttpWebConst.Http_Lobby_GetUserProfile,{uids:"" + uid,fields:"uid,headurl1,username,nickname,gender,honour,wincnt,losecnt,mascot,gift",callBack:getUserDetailInfo});
			}
			///初始化桌子信息
			AppData.getInstance().nineHalfGame.nineHalfPlayers = [];
			//设置房间信息
			var roomInfo :RoomInfo = AppData.getInstance().game.getRoomInfoByRoomId(AppData.getInstance().game.currentRoomId);
			if(roomInfo)
			{
				nineHalfView.setBaseInfo(roomInfo);
			}
		}
		/**
		 * 
		 * 删除
		 */		
		override public function onRemove():void
		{
			removeEventListeners();
			//
			super.onRemove();
		}
	}
}